part of base.http;

class ApiFactory {
  ApiFactory._internal();

  factory ApiFactory() => _instance;

  static late final ApiFactory _instance = ApiFactory._internal();

  final _map = HashMap<String, Dio>();

  Dio get dio => _map.values.first;

  createApi(String baseUrl, {List<Interceptor>? interceptor}) {
    //初始化 dio
    return _map.containsKey(baseUrl)
        ? _map[baseUrl]!
        : Dio().apply((dio) => {
              _map[baseUrl] = dio,
              dio.options = BaseOptions(
                  baseUrl: baseUrl,
                  receiveTimeout: const Duration(seconds: 6),
                  connectTimeout: const Duration(seconds: 6)),
              if (interceptor != null && interceptor.isNotEmpty)
                {dio.interceptors.addAll(interceptor)},
              dio.interceptors
                  .add(LogInterceptor(requestBody: true, responseBody: true)),
            });
  }
}
